//
//  SZLMessageListViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/5.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLMessageListViewController.h"
#import "SZLNavigationController.h"
#import "MessageTableViewCell.h"
#import <MJRefresh.h>
#import "HomeNetService.h"
#import "SZLAccountHelper.h"
#import "SZLMessageContentViewController.h"
#import "MBProgressHUD+MJ.h"
#import "SZLTabBarViewController.h"
#import "NoCommentView.h"

@interface SZLMessageListViewController ()<UITableViewDataSource,UITableViewDelegate,SZLNavigationControllerDelegate>

@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,strong) NSMutableArray *messages;
@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NoCommentView *noMessageView;

@end

@implementation SZLMessageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"消息中心";
    self.curPage = 1;
    self.sourceTab.mj_header.automaticallyChangeAlpha = YES;
    [self.view addSubview:self.sourceTab];
    [self getMessageList];
}
- (void)getMessageList
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showWaiter];
    [HomeNetService getMessageListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *messages) {
        [self hideWaiter];
        [self.sourceTab.mj_header endRefreshing];
        [self.sourceTab.mj_footer endRefreshing];
        if (self.noMessageView.superview) {
            [self.noMessageView removeFromSuperview];
        }
        if (success) {
            self.sourceTab.mj_footer.hidden = messages.count < 20;
            if (messages.count == 0&&self.curPage == 1) {
                [self.sourceTab addSubview:self.noMessageView];
            }else{
                [self hideNoDataView];
                [self.messages addObjectsFromArray:messages];
                [self.sourceTab reloadData];
            }
        }else{
            [MBProgressHUD showError:msg toView:self.view];
            if ([msg isEqualToString:NoLoginError]) {
                [self.navigationController popToRootViewControllerAnimated:YES];
                SZLTabBarViewController *barVc = (SZLTabBarViewController*)[UIApplication sharedApplication].keyWindow.rootViewController;
                barVc.selectedIndex = 0;
                [SZLAccountHelper clearAlreadyInfo];
                [SZLAccountHelper shouldLogin];
            }
        }
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messages.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MessageTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.messages[indexPath.row];
    return cell;
}

//- (BOOL)szlNavigationControllershouldPopItem:(SZLNavigationController *)navi
//{
//    if (self.messageBack) {
//        self.messageBack();
//    }
//    return YES;
//}

- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight)];
        _sourceTab.dataSource = self;
        _sourceTab.delegate = self;
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([MessageTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([MessageTableViewCell class])];
        _sourceTab.rowHeight = UITableViewAutomaticDimension;
        _sourceTab.estimatedRowHeight = 100;
        _sourceTab.backgroundColor = RGBColor(239, 239, 244);
        _sourceTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        _sourceTab.tableFooterView = [[UIView alloc]init];
        __unsafe_unretained typeof(self) weakSelf = self;
        _sourceTab.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.curPage = 1;
            [weakSelf.messages removeAllObjects];
            [self getMessageList];
        }];
        _sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            weakSelf.curPage++;
            [self getMessageList];
        }];
    }
    return _sourceTab;
}
- (NSMutableArray *)messages
{
    if (!_messages) {
        _messages = [NSMutableArray array];
    }
    return _messages;
}
- (NoCommentView *)noMessageView
{
    if (!_noMessageView) {
        _noMessageView = [NoCommentView noMessageView];
        _noMessageView.frame = CGRectMake((ScreenW-80)/2.0, (ScreenH-64-49)*0.5-80, 80, 160);
    }
    return _noMessageView;
}

@end
